package com.bysj.agent.controller;

import bean.ChatMessage;
import com.bysj.agent.assistant.UserAssistant;

import com.bysj.agent.service.MessageService;
import com.bysj.agent.tools.UserTools;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.service.AiServices;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "二手客服")
@RestController
@RequestMapping("/agent2")
public class test {
    @Autowired
    private OpenAiChatModel openAiChatModel;

    @Autowired
    private ChatMemoryProvider chatMemoryProvider;

    @Autowired
    private UserTools userTools;

    @Autowired
    private ContentRetriever contentRetriever;

    @PostMapping("/user/chat")
    public String userChat(@RequestBody ChatMessage chatmessage){
        System.out.println("1111\n\n\n\n22222");
        try{
            MessageService.setMessage(chatmessage);
            UserAssistant userAssistant = AiServices.builder(UserAssistant.class)
                    .chatLanguageModel(openAiChatModel)
                    .chatMemoryProvider(chatMemoryProvider)
                    .tools(userTools)
                    .contentRetriever(contentRetriever)
                    .build();

            String chat = userAssistant.chat(Long.valueOf(chatmessage.getChatListId()),chatmessage.getContent());
            return chat;
        }finally {
            MessageService.clearMessage();
        }

    }
}
